python - 在刮表的第一列添加标题
问题描述
我目前正在从事一个学校项目,我正在从一个自行车网站上抓取结果。我设法构建了爬虫来遍历包含结果的所有 url。我想将活动标题添加到每个表格的第一列,但遇到了一些困难。
这是我的代码:
# list of needed packages
import requests
from bs4 import BeautifulSoup
import time
import csv
# create string of urls to scrape
urls = ['https://cqranking.com/men/asp/gen/race.asp?raceid=36151', 'https://cqranking.com/men/asp/gen/race.asp?raceid=36151']
# Generates a csv-file named cycling_results.csv, with wanted headers
with open('cycling_results.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=';')
writer.writerow(['Start', 'Rank', '', '', '', 'Name', '', 'Team', '', 'Time', '', 'Points'])
# loop through all urls in the array
for url in urls:
time.sleep(2)
response = requests.get(url)
data = response.content
soup = BeautifulSoup(data, 'html.parser')
# Find the title of the racing event
titles = soup.find('title')
for title in titles:
writer.writerow(title)
tables = soup.find_all('table')
for table in tables:
rows = table.find_all('tr')
for row in rows:
csv_row = []
columns = row.find_all('td')
for column in columns:
csv_row.append(column.get_text())
writer.writerow(csv_row)
在下一个阶段,我将添加代码以删除空行。
谢谢问候凯文
解决方案
这段代码应该是
titles = soup.find('title')
for title in titles:
writer.writerow(title)
---->
titles = soup.find('title')
writer.writerow([title.text])
find 只返回一个元素,而不是元素列表。写元素文本或你想要的信息,但不是完整的元素
推荐阅读
- git - 队友没有看到“结帐”而是“结帐为”
- database - 用于 Wordpress 的 Ultimate Membership Pro 插件
- java - 出现意外错误(类型=未找到,状态=404)。- 春季Java
- python - 带有管理器和异步方法的 multiprocessing.pool
- proxy - 使用 Istio 出口网关或 ServiceEntry 将 HTTP 请求代理到外部代理
- javascript - Jquery 使用不同的方法来访问元素值
- java - OpenCSV 标头缺少必填字段 []
- node.js - 在 AWS lambda node.js 中获取秘密
- javascript - 发送给除特定客户以外的所有客户
- http - 设置 Clear-Site-Data 标头时的预期行为?