前言

没啥好说的,第二章,爬虫。

爬虫部分/数据清洗部分

一、第一题

BeautifulSoup过时了,改使用BeautifulSoup4进行数据清洗,维基百科进不去,改成百度。
发现后面需要那个网址的表格,蚌埠住了 ,开代理!

  1. # 构造 BeautifulSoup,使用html.parser 解析 ,还有lxml解析格式变成树节点格式
  2. soup = BeautifulSoup(html_doc, 'html.parser')

需要使用代理,python requests 代理代码如下:
ssr 需要修改:
代理模式:PAC
代理规则:全局

  1. url = "http://www.google.com/"
  2. proxies = {
  3. 'https': 'https://127.0.0.1:1080',
  4. 'http': 'http://127.0.0.1:1080'
  5. }
  6. content = requests.get(url=url, proxies=proxies, timeout=2)
  7. # 如果没有报错,说明正常
  8. print(content)

浏览器验证
image.png
代码验证:
image.png

修改题目给出的代码:

  1. #url = "http://www.baidu.com"
  2. url="https://en.wikipedia.org/wiki/List_of_U.S._states_by_historical_population"
  3. proxies = {
  4. 'https': 'https://127.0.0.1:1080',
  5. 'http': 'http://127.0.0.1:1080'
  6. }
  7. # 设置代理,
  8. r = requests.get(url, proxies=proxies,timeout=3,verify=False)
  9. print(r)

PS: 给出的代码是错误的,由于网页变动或者其他,并不能得出结果,具体已经修改,问题是,居然花了一下午?蛋疼,坑真多!