【发布时间】:2018-04-11 10:23:04
【问题描述】:
我用 python 编写了一个脚本来从网页中抓取一些内容。在解析数据时,刮板做得很好。有两个字段可以抓取name 和data,每个字段都包含项目列表。但是,当我打印它时,结果会变得混乱,因为我此时无法正确打印它们。
这是我迄今为止尝试过的:
import requests, csv
from bs4 import BeautifulSoup
LINK = 'http://active.boeing.com/doingbiz/d14426/geoprocess.cfm?ProcessCode=000&pageID=m20487&Country=AllLocations&State='
def get_item(url):
res = requests.get(url).text
soup = BeautifulSoup(res,"lxml")
name = [item.find_next_sibling().text for item in soup.select("strong")]
table = soup.select('table[cellspacing="1"]')[0]
for items in table.select("tr")[1:]:
data = [item.get_text(strip=True) for item in items.select("td")]
print(name,data) #this is where I need to twitch the code to get them printed like how it should be
with open("itemresults.csv","a",newline="") as infile:
writer = csv.writer(infile)
writer.writerow(name,data) #I can't write them like so but if I try like [name,data] this the results are messy
if __name__ == '__main__':
get_item(LINK)
为了清楚起见:name 变量中的列表应打印一次,但它们的打印速度与data 变量中的列表保持同步。
As they are big enough to show how the expected result look like, I'm trying with a demo:
"1,2,3" are within "name".
I wish to get them printed like below:
1 2 3 q w e
a s d
c x r
They are printed like the following instead:
1 2 3 q w e
1 2 3 a s d
1 2 3 c x r
底线是:
1. I wish to get them printed accordingly and
2. Write in a csv file in the right way
【问题讨论】:
-
['000', '000', 'Boeing Information Only', 'Boeing Info Only', 'Boeing Information Only'] ['AUSTRIA', '', 'BE10410486', 'MAGNA STEYR'] ['CHINA', '', 'BE10409781', 'FESHER AVIATION COMPONENTS ZHENJIANG CO LTD'] ['CHINA', '', 'BE10050454', 'SHENYANG AIRCRAFT CORP']这是你想要的输出吗? -
请查看编辑。谢谢。
-
添加固定代码看看是不是你想要的
-
感谢@toheedNiaz,您的回答。问题是我喜欢并排获取
name和data的内容,而不是像我在上面尝试显示的那样连续获取。谢谢。 -
该数据将毫无意义的 csv 文件。你检查生成的 csv 文件了吗?
标签: python python-3.x list csv web-scraping