当前位置:网站首页>爬虫代码基础教学
爬虫代码基础教学
2022-07-03 06:20:00 【pjiang000】
from http.client import ResponseNotReady
import json
from unicodedata import name
import requests
from lxml import etree
import csv
import xlwt
# base_url = 'https://www.basketball-reference.com'
base_url = https://www.baidu.com
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36',
}
response = requests.get(base_url, headers=headers)
html = etree.HTML(response.text)
url_list = html.xpath('//*[@id="site_menu"]/ul/li[2]/div/a/@href')
team_my_names = html.xpath('//*[@id="site_menu"]/ul/li[2]/div/a/text()')
teams_list = url_list
# print(teams_list)
for h in range(len(teams_list)):
base_url = 'https://www.basketball-reference.com' + teams_list[h]
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36',
}
xls = xlwt.Workbook()
sht1 = xls.add_sheet('Sheet1')
response = requests.get(base_url, headers=headers)
html = etree.HTML(response.text)
url_list = html.xpath('//*[@id="roster"]/tbody/tr/td[1]/a/@href')
names = html.xpath('//*[@id="roster"]/tbody/tr/td[1]/a')
# print(url_list)
# print(names)
name_lst = []
for j in range(len(names)):
name_lst.append(names[j].text)
names = name_lst
person_list = []
for j in range(len(url_list)):
person_list.append('https://www.basketball-reference.com' + url_list[j])
url_list = person_list
print(team_my_names[h], end=": ")
file_name = 'files/' + team_my_names[h] + ".xls"
count = 0
print()
for i in range(len(url_list)):
print(names[i], end=',')
response = requests.get(url_list[i], headers=headers)
html = etree.HTML(response.text)
year_lst = html.xpath('//*[@id="per_game"]/tbody/tr/th/a/text()')
team_name = html.xpath('//*[@id="per_game"]/tbody/tr/td[@data-stat="team_id"]//text()')
for j in range(len(year_lst)):
sht1.write(count, 0, names[i])
sht1.write(count, 1, year_lst[j])
sht1.write(count, 2, 'NULL' if len(team_name) < j else team_name[j])
count += 1
xls.save(file_name)
print()
边栏推荐
- 2022 CISP-PTE(三)命令执行
- 认识弹性盒子flex
- Apifix installation
- Local rviz call and display of remote rostopic
- Une exploration intéressante de l'interaction souris - pointeur
- phpstudy设置项目可以由局域网的其他电脑可以访问
- Reinstalling the system displays "setup is applying system settings" stationary
- How matlab modifies default settings
- “我为开源打榜狂”第一周榜单公布,160位开发者上榜
- opencv
猜你喜欢
CKA certification notes - CKA certification experience post
使用 Abp.Zero 搭建第三方登录模块(一):原理篇
IE browser flash back, automatically open edge browser
Une exploration intéressante de l'interaction souris - pointeur
Support vector machine for machine learning
GPS坐标转百度地图坐标的方法
Scripy learning
Cesium 点击获三维坐标(经纬度高程)
Oauth2.0 - explanation of simplified mode, password mode and client mode
Paper notes vsalm literature review "a comprehensive survey of visual slam algorithms"
随机推荐
使用 Abp.Zero 搭建第三方登录模块(一):原理篇
Interface test weather API
技术管理进阶——你了解成长的全貌吗?
Some thoughts on machine learning
Page text acquisition
Condition annotation in uni-app realizes cross segment compatibility, navigation jump and parameter transfer, component creation and use, and life cycle function
Cesium entity(entities) 实体删除方法
Simple password lock
Use abp Zero builds a third-party login module (I): Principles
表达式的动态解析和计算,Flee用起来真香
SSH link remote server and local display of remote graphical interface
Oauth2.0 - use database to store client information and authorization code
PHP用ENV获取文件参数的时候拿到的是字符串
Project summary --2 (basic use of jsup)
Scripy learning
Difference between shortest path and minimum spanning tree
Zhiniu stock project -- 04
【开源项目推荐-ColugoMum】这群本科生基于国产深度学习框架PaddlePadddle开源了零售行业解决方案
Pytorch exercise items
Heap sort and priority queue