当前位置:网站首页>爬虫代码基础教学
爬虫代码基础教学
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()
边栏推荐
- Mysql5.7 group by error
- Kubernetes notes (10) kubernetes Monitoring & debugging
- Jackson: what if there is a lack of property- Jackson: What happens if a property is missing?
- Oauth2.0 - use database to store client information and authorization code
- 【开源项目推荐-ColugoMum】这群本科生基于国产深度学习框架PaddlePadddle开源了零售行业解决方案
- Oauth2.0 - Introduction and use and explanation of authorization code mode
- Oauth2.0 - explanation of simplified mode, password mode and client mode
- Openresty best practices
- Kubesphere - Multi tenant management
- What's the difference between using the Service Worker Cache API and regular browser cache?
猜你喜欢

Important knowledge points of redis

Project summary --01 (addition, deletion, modification and query of interfaces; use of multithreading)

IE browser flash back, automatically open edge browser

Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster

有意思的鼠标指针交互探究

YOLOV1学习笔记

深入解析kubernetes controller-runtime

Project summary --04

【系统设计】邻近服务

远端rostopic的本地rviz调用及显示
随机推荐
What's the difference between using the Service Worker Cache API and regular browser cache?
Project summary --04
Characteristics and isolation level of database
Time format record
Paper notes vsalm literature review "a comprehensive survey of visual slam algorithms"
Zhiniu stock project -- 04
UNI-APP中条件注释 实现跨段兼容、导航跳转 和 传参、组件创建使用和生命周期函数
Learning notes -- principles and comparison of k-d tree and IKD tree
轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
opencv
After the Chrome browser is updated, lodop printing cannot be called
Decision tree of machine learning
Push box games C #
Cesium 点击获取模型表面经纬度高程坐标(三维坐标)
Use @data in Lombok to simplify entity class code
Creating postgre enterprise database by ArcGIS
Chapter 8. MapReduce production experience
第8章、MapReduce 生产经验
CKA certification notes - CKA certification experience post
Floating menu operation