使用selenium库爬取多家公司多页的数据,为什么无法使用selenium实现翻页爬取?
# ============================================================================= # 9.2 东方财富网数据挖掘实战 实战2 第二题 # ============================================================================= from selenium import webdriver import re def dongfang(company, num): browser = webdriver.Chrome() # 这里改成了有界面模式,方便调试代码 url = 'http://so.eastmoney.com/news/s?keyword=' + company browser.get(url) browser.find_element("xpath",'//*[@id="kw"]').send_keys('str(num)') browser.find_element("xpath",'//*[@id="kw"]').send_keys(Keys.ENTER) data = browser.page_source browser.quit() # print(data) # 如果正则发生变化,可以通过打印源代码进行调试 p_title = '
(.*?)' p_href = '' p_date = '
(.*?) - ' title = re.findall(p_title, data) href = re.findall(p_href, data) date = re.findall(p_date, data, re.S) for i in range(len(title)): title[i] = re.sub('<.*?>', '', title[i]) date[i] = date[i].split(' ')[0] print(str(i+1) + '.' + title[i] + ' - '+ date[i]) print(href[i]) companys = ['阿里巴巴', '万科集团', '百度集团', '腾讯', '京东'] for company in companys: for i in range(3): # 这里一共爬取了3页 dongfang(company, i+1) # i是从0开始的序号,所以要写成i+1表示第几页 print("第" + str(i+1) + "页" + company + '东方财富网爬取成功') # i是从0开始的序号,所以写i+1 time.sleep(3) # 不要爬太快,爬太快会被百度反爬
无
可能是关于翻页的两行代码不正确,希望得到正确的代码
切换到该网页:https://so.eastmoney.com/news/s?keyword=%E9%98%BF%E9%87%8C%E5%B7%B4%E5%B7%B4
按F12打开开发者工具,然后Ctrl+F打开搜索,搜索你设置的xpath://*[@id="kw"]
会发现找到0个元素,说明xpath写法有误(你的报错也提示了,说Unable to locate element: "{...
):
find,并click下图框住的这些element:
代码如下:
xpath_str = '//*[@class="c_pager"]/div[1]/a[%d]'%(num+1)
browser.find_element("xpath",xpath_str).click()
如有问题,请继续追问。
你的Xpath有问题,另外你的页码没有生效,你需要找正确的xpath,你目前是在找input
from selenium import webdriver
import re
import time
def dongfang(company, num):
browser = webdriver.Chrome() # 这里改成了有界面模式,方便调试代码
url = 'http://so.eastmoney.com/news/s?keyword=' + company
browser.get(url)
browser.find_element("xpath",'/html/body/div[1]/div[3]/div[1]/div[5]/div/form/input').send_keys(str(num))
browser.find_element("xpath",'/html/body/div[1]/div[3]/div[1]/div[5]/div/form/input').send_keys(webdriver.common.keys.Keys.ENTER)
data = browser.page_source
browser.quit()
# print(data) # 如果正则发生变化,可以通过打印源代码进行调试
p_title = '<div class="news_item_t"[^>]*><a[^>]*>(.*?)</a>'
p_href = '<div class="news_item_url"[^>]*><a[^>]*>(.*?)</a></div>'
p_date = '<div class="news_item_c"><span class="news_item_time">(.*?) - </span>'
title = re.findall(p_title, data)
href = re.findall(p_href, data)
date = re.findall(p_date, data, re.S)
for i in range(len(title)):
title[i] = re.sub('<.*?>', '', title[i])
date[i] = date[i].split(' ')[0]
print(str(i+1) + '.' + title[i] + ' - '+ date[i])
print(href[i])
companys = ['阿里巴巴', '万科集团', '百度集团', '腾讯', '京东']
for company in companys:
for i in range(3): # 这里一共爬取了3页
dongfang(company, i+1) # i是从0开始的序号,所以要写成i+1表示第几页
print("第" + str(i+1) + "页" + company + '东方财富网爬取成功') # i是从0开始的序号,所以写i+1
time.sleep(3) # 不要爬太快,爬太快会被百度反爬
你昨天的帖子,没看吗我都说网页没加载要sleep一下,合着你啥都没看
【为什么无法使用selenium实现翻页爬取】
分析和思路
1、页面没加载完
使得当前页面下拉:web.execute_script(“window.scrollTo(0, document.body.scrollHeight);”)
2、页面翻页后,数据未同步
selenium:操作翻页以后,selenium默认还在操作原来页面,让它改变当前操作页:web.switch_to.window(web.window_handles[-1])
提供参考链接,期望对你有所帮助:https://blog.csdn.net/myscratch/article/details/115899339
【最后,你也可参考下问答社区中已采纳的同类问题,他是爬取淘宝,可以借鉴对比看一下,链接:https://ask.csdn.net/questions/7417857
博主参考这个链接,希望对你有帮助
https://b23.tv/mvpFg9E
可以实现翻页,有时需要设置一个sleep让页面加载,之后运用clock点击你想翻页的页面翻页按键。