|
|
3 years ago | |
|---|---|---|
| README.md | 3 years ago | |
README.md
crawler
#Spider正常运行代码 import requests url = "https://www.bitpush.news/covid19/" headers = { "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36" } response = requests.get(url, headers=headers) html = response.text html from lxml import etree parse = etree.HTMLParser(encoding='utf-8') doc = etree.HTML(html) state = doc.xpath('//div[@class="table_container"]//tbody/tr/td/span/text()')[101:] person = doc.xpath('//div[@class="table_container"]//tbody/tr/td[2]/text()')[101:] person = [x.replace(",", "") for x in person] death = doc.xpath('//div[@class="table_container"]//tbody/tr/td[3]/text()') death = [x.replace(",", "") for x in death] message = list(zip(state, person, death)) message print(message) import csv with open("content.csv", "w") as f: w = csv.writer(f) w.writerows(message) import pandas as pd df = pd.read_csv("content.csv", names=["state", "person", "death"],encoding='GB2312') df.head() df.info() df1 = df.drop(0).head(10) df1 import matplotlib.pyplot as plt %matplotlib inline plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['figure.figsize'] = (10, 5) x = df1["state"].values y = df1["death"].values plt.bar(x, y) plt.xlabel("州",fontsize=14) plt.ylabel("死亡人数",fontsize=14) plt.show()
#头哥正常运行代码 import requests url = "https://www.bitpush.news/covid19/" headers = { "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36" } response = requests.get(url, headers=headers) html = response.text html from lxml import etree parse = etree.HTMLParser(encoding='utf-8') doc = etree.HTML(html) state = doc.xpath('//div[@class="table_container"]//tbody/tr/td/span/text()')[101:] person = doc.xpath('//div[@class="table_container"]//tbody/tr/td[2]/text()')[101:] person = [x.replace(",", "") for x in person] death = doc.xpath('//div[@class="table_container"]//tbody/tr/td[3]/text()') death = [x.replace(",", "") for x in death] message = list(zip(state, person, death)) message print(message) import csv with open("content.csv", "w") as f: w = csv.writer(f) w.writerows(message) import pandas as pd df = pd.read_csv("content.csv", names=["state", "person", "death"]) df.head() df.info() df1 = df.drop(0).head(10) df1 import matplotlib.pyplot as plt %matplotlib inline plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['figure.figsize'] = (10, 5) x = df1["state"].values y = df1["death"].values plt.bar(x, y) plt.xlabel("州",fontsize=14) plt.ylabel("死亡人数",fontsize=14) plt.show()