如何解决使用 Python 和 Selenium 抓取 Instagram 照片链接
我正在尝试为某些主题标签抓取照片链接。 Instagram 永远不会停止滚动提要,所以我想向下滚动五次,然后抓取我能看到的图片的所有链接,但每次我的机器人只抓取 48 个链接。
driver.get('https://www.instagram.com/explore/tags/'+hashtag)
SCROLL_PAUSE_TIME = 0.5
# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")
x=1
while x<=5:
# Scroll down to bottom
driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")
# Wait to load page
time.sleep(SCROLL_PAUSE_TIME)
#sleep(1)
# Calculate new scroll height and compare with last scroll height
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
x=x+1
elems =driver.find_elements(By.TAG_NAME,'a')
links = [elem.get_attribute('href') for elem in elems]
r = re.compile(".*/p/.")
links = list(filter(r.match,links))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。