python抓取网页数据(谷歌浏览器用的python抓取页面数据:用)

优采云 发布时间: 2021-09-29 12:07

  python抓取网页数据(谷歌浏览器用的python抓取页面数据:用)

  以前,python用于获取页面数据:

  1 url = "http://xxxxxx"

2 res_text = json.loads(urllib2.urlopen(urllib2.Request(server_url_text)).read())

3 print('res_text')

  稍后,调用查看页面返回“请打开浏览器的JavaScript并刷新浏览器”。查看后,将添加一个cookie。添加cookie后,它将恢复正常

  url = "http://xxxxxx"

headers = {'User-Agent': xxx", 'Cookie':xxx",}

res_text = json.loads(urllib2.urlopen(urllib2.Request(server_url_text,headers=headers)).read())

print('res_text')

  然而,cookie经常改变,所以我们使用webdriver以不同的方式获取页面数据

   1 from selenium import webdriver

2 import time,json

3

4 driver = webdriver.Chrome()

5 driver.get('xxxx')

6 time.sleep(7)

7 res = driver.find_element_by_xpath('xxxxxx')

8 s = json.loads(res.text)

9 driver.close()

10

11 print(s,type(s))

  注:以上方法使用谷歌浏览器,需要提前安装谷歌浏览器及相应的驱动程序chromedriver;其他浏览器也可用

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线