当前位置:网站首页>爬虫代码基础教学
爬虫代码基础教学
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()
边栏推荐
- opencv鼠标键盘事件
- Difference between shortest path and minimum spanning tree
- Kubernetes notes (10) kubernetes Monitoring & debugging
- Project summary --01 (addition, deletion, modification and query of interfaces; use of multithreading)
- phpstudy设置项目可以由局域网的其他电脑可以访问
- UNI-APP中条件注释 实现跨段兼容、导航跳转 和 传参、组件创建使用和生命周期函数
- 从 Amazon Aurora 迁移数据到 TiDB
- 2022 CISP-PTE(三)命令执行
- IE browser flash back, automatically open edge browser
- 简易密码锁
猜你喜欢
![[5g NR] UE registration process](/img/e3/f881d51fba03010de8c45ea480f3f0.png)
[5g NR] UE registration process

Phpstudy setting items can be accessed by other computers on the LAN

ThreadLocal的简单理解

Svn branch management

phpstudy设置项目可以由局域网的其他电脑可以访问

SQL实现将多行记录合并成一行

Use abp Zero builds a third-party login module (I): Principles

Selenium ide installation recording and local project maintenance

Tabbar settings

.NET程序配置文件操作(ini,cfg,config)
随机推荐
Condition annotation in uni-app realizes cross segment compatibility, navigation jump and parameter transfer, component creation and use, and life cycle function
. Net program configuration file operation (INI, CFG, config)
[system design] proximity service
【C#/VB.NET】 将PDF转为SVG/Image, SVG/Image转PDF
Numerical method for solving optimal control problem (I) -- gradient method
Migrate data from Amazon aurora to tidb
Shell conditional statement
Floating menu operation
使用conda创建自己的深度学习环境
YOLOV2学习与总结
How matlab modifies default settings
Simple password lock
Zhiniu stock -- 03
Scroll view specifies the starting position of the scrolling element
Oauth2.0 - explanation of simplified mode, password mode and client mode
Learning notes -- principles and comparison of k-d tree and IKD tree
2022 CISP-PTE(三)命令执行
Introduction to software engineering
輕松上手Fluentd,結合 Rainbond 插件市場,日志收集更快捷
2022 cisp-pte (III) command execution