以下代码有错误修改:from bs4 import BeautifulSoup import requests import openpyxl def getHTMLText(url): try: r=requests.get(url) r.raise_for_status() r.encoding=r.apparent_encoding return r.text except: r="fail" return r def find2(soup): lsauthors=[] for tag in soup.find_all("td"): for img in tag.select("img[title]"): h=[] h=img["title"] lsauthors.append(h) def find3(soup): lsbfl=[] for tag in soup.find_all("td")[66:901]: #print(tag) bfl=[] bfl=tag.get_text() bfl=bfl.strip() lsbfl.append(bfl) return lsbfl if __name__ == "__main__": url= "https://www.kylc.com/stats/global/yearly/g_population_sex_ratio_at_birth/2020.html" text=getHTMLText(url) soup=BeautifulSoup(text,'html.parser') find2(soup) lsbfl=find3(soup) workbook=openpyxl.Workbook() worksheet = workbook.create_sheet('排名',index=0) project=['排名','国家/地区','所在洲','出生人口性别比'] rank=[] a=2 b=3 c=1 for i in range(1,201,1): rank.append(i) for i in range(len(project)): worksheet.cell(row=1, column=i + 1).value = project[i] for i in range(len(rank)): worksheet.cell(row=i + 2, column=1).value = rank[i] for i in range(200): worksheet.cell(row=i + 2, column=2).value = lsbfl[c] c=c+4 for i in range(200): worksheet.cell(row=i + 2, column=3).value = lsbfl[a] a=a+4 for i in range(200): worksheet.cell(row=i + 2, column=4).value = lsbfl[b] b=b+4 wb=workbook wb.save('D:\世界各国出生人口性别比.xlsx') import numpy as np import matplotlib.pyplot as plt import matplotlib labels = ['United States','China','Ukraine','Japan','Russia','Others'] values = np.array([11,69,9,23,20,68]) fig = plt.figure() sub = fig.add_subplot(111) sub.pie(values, labels=labels, explode=[0,0,0,0,0,0.05], autopct='(%.1f)%%', shadow = True, wedgeprops = dict( edgecolor='k', width=0.85)) sub.legend() fig.tight_layout() labels2=['0-100','100-200','>200'] people_means=[140,43,17] x=np.arange(len(labels2)) width=0.50 fig,ax=plt.subplots() rects=ax.bar(x,people_means,width,label='Number of matches') ax.set_ylabel('sum') ax.set_title('People compare') ax.set_xticks(x) ax.set_xticklabels(labels2) ax.legend() plt.show()
时间: 2023-06-03 15:03:23 浏览: 115
修改后的代码如下:
from bs4 import BeautifulSoup
import requests
import openpyxl
def getHTMLText(url):
try:
r=requests.get(url)
r.raise_for_status()
r.encoding=r.apparent_encoding
return r.text
except requests.exceptions.RequestException as e:
return "fail: {}".format(str(e))
def find2(soup):
lsauthors=[]
for tag in soup.find_all("td"):
for img in tag.select("img[title]"):
h=img["title"]
lsauthors.append(h)
return lsauthors
def find3(soup):
lsbfl=[]
for tag in soup.find_all("td")[66:901]:
# do something
pass
return lsbfl
阅读全文