web-scraping - 将爬网数据导出到 csv
问题描述
我想抓取所有船只详细信息页面并将它们导出到具有表头的 csv:标签 = [“也称为:”,“建造年份:”,“官方编号:”,“建造时间:”,“船只类型: ”、“其他船只类型:”、“船体材料:”、“建造者名称:”、“原始所有者和位置:”、“长度:”、“梁:”、“深度:”、“吨位(总): ", "吨位(净):", "联系方式"]
我尝试了一些技术,但没有奏效,我的爬行代码是..
import requests
from bs4 import BeautifulSoup
baseurl ='https://greatlakeships.org/'
headers= {'User-Agent': 'Mozilla/5.0'}
productlinks = [] #put all item in this array
for x in range(1,10 ): # set page range
response = requests.get(f'https://greatlakeships.org/results?bl=and&st=kw&q2=text%3A%28%2A%3A%2A%29&rows=20&sort=titleSort%20asc&p={x}') #url of next page
soup = BeautifulSoup(response.content, 'html.parser')
productlist =soup.find_all('ul', class_='single')
#loop to get all href from ul
for item in productlist:
for link in item.find_all('a', href = True):
productlinks.append(baseurl + link['href'])
print(len(productlinks))
testlink = 'https://greatlakeships.org/3721293/data?n=1'
response = requests.get(testlink, headers=headers)
soup = BeautifulSoup(response.content, 'html.parser')
tablevalue = soup.find_all('dd') #tablevalue = soup.find_all('dd').text.strip() not working
#print(tablevalue)
它是我的代码(仍然不完整),它通过名称中的链接获取每艘船只的详细信息,例如https://greatlakeships.org/3721293/data?n=1但我不知道如何将这些数据导出到 csv 。我想要一张表中所有船只的详细信息(csv 格式)。代码中的倒数第二行 #tablevalue = soup.find_all('dd').text.strip() 也不起作用。
解决方案
推荐阅读
- unity3d - Unity 2018.1 WebGL 使用 icsharpcode.sharpziplib 在 CommitUpdate 上失败
- r - Identify patterns and turn them as a new column
- weblogic - Web Logic Deployment
- html - css wordwrap,它是如何工作的?
- .net - how to integrate bot into skype
- python - How to train CoreNlp to detect names in Python?
- android - NoSuchMethodError: 没有虚方法 decrementAndGet
- php - Fill RTF placeholder with php string that contains line breaks and tabs
- ruby-on-rails - 访问 database.yml 中的自定义类
- php - Slim Route 似乎重置了静态变量