python爬取英文名
爬取某网站名字的详细信息
部分代码
# 翻页 # 每页50条数据 pageNum = 0 if counts % 50 == 0: pageNum = counts / 50 else: pageNum = counts // 50 + 1 while page <= pageNum: page += 1 url = url + "/page/{}/".format(page) print("========第{}页============".format(page)) get_contents(url, page)
获取名细
def get_detail(url): html = requests.get(url, headers=headers, verify=False) soup = BeautifulSoup(html.text, "lxml") name = soup.find(‘div‘, attrs={‘class‘: ‘single_baby_name_title‘}).find(‘h1‘).text # 获取name s = soup.find(‘div‘, attrs={‘class‘: ‘single_baby_name_description‘}).find_all(‘span‘) # 获取Meaning Meaning = s[0].text # 获取Meaning Gender = s[1].text # 获取Gender Origin = s[3].text # 获取Origin
保存到csv
# 保存数据到csv with open("baby_name.csv", ‘a+‘, encoding="utf-8-sig", newline=‘‘) as f: csv_writer = csv.writer(f) csv_writer.writerow([name, Meaning, Gender, Origin]) print("=========保存数据成功==========")
实验中慢慢摸索,总结出不少东西
相关推荐
HeyShHeyou 2020-11-17
86193952 2020-10-27
小木兮子 2020-11-11
seanzed 2020-10-15
huangliuyu00 2020-09-24
libaoshan 2020-09-11
zhangpan 2020-09-10
chongxiaocheng 2020-08-16
xcguoyu 2020-08-15
Qizonghui 2020-08-02
ldcwang 2020-07-26
mqfcu 2020-07-21
jeason 2020-07-20
sunzhihaofuture 2020-07-19
knightwatch 2020-07-19
点滴技术生活 2020-07-19
Reiki 2020-07-06