简体   繁体   English

使用Selenium Python进行网页搜索[Twitter + Instagram]

[英]Web Scraping with Selenium Python [Twitter + Instagram]

I am trying to web scrape both Instagram and Twitter based on geolocation. 我正在尝试基于地理定位来抓取Instagram和Twitter。 I can run a query search but I am having challenges in reloading the web page to to more and store the fields to data-frame. 我可以运行查询搜索但是我在将网页重新加载到更多并将字段存储到数据框时遇到了挑战。

I did find couple of examples for web scraping twitter and Instagram without API keys. 我确实找到了几个没有API密钥的网页抓取Twitter和Instagram的例子。 But they are with respect to #tags keywords. 但它们与#tags关键字有关。

I am trying to scrape with respect to geo location and between old dates. 我试图抓住地理位置和旧日期之间。 so far I have come this far with writing code in python 3.X and all the latest versions of packages in anaconda. 到目前为止,我已经在python 3.X中编写代码以及anaconda中所有最新版本的软件包。

'''
    Instagram - Components
    "id": "1478232643287060472", 
     "dimensions": {"height": 1080, "width": 1080}, 
     "owner": {"id": "351633262"}, 
     "thumbnail_src": "https://instagram.fdel1-1.fna.fbcdn.net/t51.2885-15/s640x640/sh0.08/e35/17439262_973184322815940_668652714938335232_n.jpg", 
     "is_video": false, 
     "code": "BSDvMHOgw_4", 
     "date": 1490439084, 
     "taken-at=213385402"
     "display_src": "https://instagram.fdel1-1.fna.fbcdn.net/t51.2885-15/e35/17439262_973184322815940_668652714938335232_n.jpg", 
     "caption": "Hakuna jambo zuri kama kumpa Mungu shukrani kwa kila jambo.. \ud83d\ude4f\ud83c\udffe\nIts weekend\n#lifeistooshorttobeunhappy\n#Godisgood \n#happysoul \ud83d\ude00", 
     "comments": {"count": 42}, 
     "likes": {"count": 3813}}, 
'''


import selenium
from selenium import webdriver
#from selenium import selenium
from bs4 import BeautifulSoup
import pandas

#geotags = pd.read_csv("geocodes.csv")
#parmalink = 
query = geocode%3A35.68501%2C139.7514%2C30km%20since:2016-03-01%20until:2016-03-02&f=tweets

twitterURL = 'https://twitter.com/search?q=' + query
#instaURL = "https://www.instagram.com/explore/locations/213385402/"


browser = webdriver.Firefox()
browser.get(twitterURL)
content = browser.page_source

soup = BeautifulSoup(content)
print (soup)

For Twitter Search Query I am getting syntax error 对于Twitter搜索查询,我收到语法错误

For Instagram I am not getting any error but I am not able to reload for more posts and write back to csv dataframe. 对于Instagram我没有收到任何错误,但我无法重新加载更多帖子并回写csv数据帧。

I am also trying to search with latitude and longitude search in both Twitter and Instagram. 我也试图在Twitter和Instagram上搜索纬度和经度搜索。

I have a list of geo coordinates in csv I can use that input or can write a query for search. 我在csv中有一个地理坐标列表我可以使用该输入或可以编写搜索查询。

Any way to complete the scraping with location will be appreciated. 任何方式来完成与位置的刮削将是值得赞赏的。

Appreciate the help !! 感谢帮助!!

I managed to make it work using requests . 我设法使用requests使其工作。 Your code would look something like this: 您的代码看起来像这样:

from bs4 import BeautifulSoup
import requests

query = "geocode%3A35.68501%2C139.7514%2C30km%20since:2016-03-01%20until:2016-03-02&f=tweets"

twitter = 'https://twitter.com/search?q=' + query

content = requests.get(twitter)
soup = BeautifulSoup(content.text)

print(soup)

Then you can use the soup object to parse what you need. 然后你可以使用soup对象来解析你需要的东西。 The same thing should work for Instagram, if your query is correct. 如果你的查询是正确的,同样的事情应该适用于Instagram。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM