在美丽的汤正则表达式列表中的Python循环

时间:2018-03-03 06:14:09

标签: regex python-3.x loops beautifulsoup

当我运行下面的代码时,我得到三个列表,一个在另一个下面垂直。 我希望它们是水平的,用逗号分隔(类似于最后一个打印列表语句,其中数据用逗号分隔)。 我尝试重新排列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&sectionId=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)

3 个答案:

答案 0 :(得分:1)

看看List Comprehensions

此外,在BeautifulSoup中使用正则表达式时,您不需要.*来匹配任何字符。

用它来得到你想要的东西:

regexQ = re.compile('Date1 Qty')
regexC = re.compile('Footnote')
regexV = re.compile('Date1 Val')

country = [x.text.strip() for x in soup.find_all("a", {"href": regexC})]
quantity = [x.text.strip() for x in soup.find_all("td", {"headers": regexQ})]
value = [x.text.strip() for x in soup.find_all("td", {"headers": regexV})]

total_list = [list(x) for x in zip(country, quantity, value)]
for item in total_list:
    print(item)

输出:

['World', '282,911,404', '67,284,637']
['Equatorial Guinea', '146,027,530', '40,493,766']
['Trinidad and Tobago', '136,883,464', '26,790,695']
['Japan', '410', '176']

答案 1 :(得分:1)

您可以在不使用CommandBar的情况下执行此操作。尝试以下方法来实现相同的目标。我使用了regex

使用list comprehensions

urllib

使用from urllib.request import urlopen from bs4 import BeautifulSoup res = urlopen("http://www5.statcan.gc.ca/cimt-cicm/topNCountryCommodities-marchandises?lang=eng&chapterId=27&sectionId=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") soup = BeautifulSoup(res.read(),"lxml") for items in soup.find_all(class_="ResultRow"): data = [item.get_text(" ",strip=True) for item in items.find_all(["th","td"])[1:4]] print(data)

requests

输出:

import requests
from bs4 import BeautifulSoup

res = requests.get("http://www5.statcan.gc.ca/cimt-cicm/topNCountryCommodities-marchandises?lang=eng&chapterId=27&sectionId=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")
soup = BeautifulSoup(res.text,"lxml")
for items in soup.find_all(class_="ResultRow"):
    data = [item.get_text(" ",strip=True) for item in items.find_all(["th","td"])[1:4]]
    print(data)

答案 2 :(得分:0)

尝试将您的国家/地区和其他结果折叠到列表中。

然后试试这个:

for mylist in lists:
    print(*mylist, end=", ")