我正在使用Selenium python创建一个instagram机器人。我只是想知道如何检索用户在其个人资料页面中包含的帖子的编号。这样我就可以在脚本中处理“ NO POSTS YET”情况。
谁能给我一个提示。我不需要完整的代码,也不需要提示我正确的位置。
答案 0 :(得分:1)
您可以通过xPath获取元素的文本:
function codeLatLng(lat, lng) {
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({ 'latLng': latlng }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log(results)
if (results[1]) {
//your code logic
var model = {
"userId": 'user_' + Math.random().toString(36).substr(2, 9),
"userName": fulladdress.latitude,
"botId": "Chatbot Main Tree",
"botIconUrl": "//bot-framework.azureedge.net/bot-icons-v1/bot-framework-default-8.png",
"botName": "Chatbot Main Tree",
"secret": "Secret",
"iconUrl": "//bot-framework.azureedge.net/bot-icons-v1/bot-framework-default-8.png",
"directLineUrl": "https://webchat.botframework.com/v3/directline",
"webSocketEnabled": "True"
};
var botConnection = new BotChat.DirectLine({ secret: model.secret });
BotChat.App({
directLine: botConnection,
user: { id: model.userId, name: model.userName },
//...
//other options
//...
}, document.getElementById("BotChatElement"));
botConnection.postActivity({
type: 'event',
from: { id: model.userId },
name: 'GetUserLocation',
value: model.userName
}).subscribe(function (id) { console.log('UserLocation: "' + model.userName + '" sent'); });
}
else {
alert("No results found");
}
}
else {
alert("Geocoder failed due to: " + status);
}
});
}
答案 1 :(得分:1)
我更喜欢使用find_element_by_xpath,因为可以在chrome中轻松找到xpath。
方法如下:右键单击->检查->右键单击->复制-> CopyXpath
xpath = '//*[@id="react-root"]/section/main/div/header/section/ul/li[1]/span/span'
if browser.find_element_by_xpath(xpath).text == '0':
pass
答案 2 :(得分:0)
根据您的任务,我写了几行内容,其中包含大量的帖子,关注者和关注者。您可以运行代码以查看结果。
browser = webdriver.Chrome()
browser.get('https://www.instagram.com/michelemessina/')
wait = WebDriverWait(browser, 5)
posts = wait.until(EC.presence_of_element_located((By.XPATH, '//li/span[text()=" posts"]/span'))).text
followers = wait.until(EC.presence_of_element_located((By.XPATH, '//li/a[text()=" followers"]/span'))).text
following = wait.until(EC.presence_of_element_located((By.XPATH, '//li/a[text()=" following"]/span'))).text
print('Posts: {}; Followers: {}; Following: {}.'.format(posts, followers, following))
希望,这会对您有所帮助。