【发布时间】:2018-03-03 06:14:08
【问题描述】:
当我运行下面的代码时,我得到三个列表,一个在另一个垂直下方。 我希望它们是水平的,用逗号分隔(类似于最后一个打印列表语句,其中数据用逗号分隔)。 我尝试重新排列 for 循环语句,我得到了各种组合,但没有像我上面描述的那样。请帮忙!
import bs4 as bs
import urllib.request
import re
sauce = urllib.request.urlopen('http://www5.statcan.gc.ca/cimt-cicm/topNCountryCommodities-marchandises?lang=eng&chapterId=27§ionId=0&refMonth=2&refYr=2017&freq=6&countryId=999&usaState=0&provId=1&arrayId=9900000&commodityId=271111&commodityName=Natural+gas%2C+liquefied&topNDefault=10&tradeType=3').read()
soup = bs.BeautifulSoup(sauce,'lxml')
regexQ = re.compile('.*Date1 Qty.*')
regexC = re.compile('.*Footnote.*')
regexV = re.compile('.*Date1 Val.*')
for countryPart in soup.findAll("a",{"href":regexC}):
Country = countryPart.text.strip()
print(Country)
for DatePart in soup.findAll("td",{"headers":regexQ}):
Quantity = DatePart.text.strip()
print(Quantity)
for ValPart in soup.findAll("td",{"headers": regexV}):
Value = ValPart.text.strip()
print(Value)
list = [Country,Quantity,Value]
print(list)
【问题讨论】:
标签: regex python-3.x loops beautifulsoup