[英]How to revert to a less "Jupiter-y" command in VSCode python interactive?
[英]How to revert a string into a command?
我实际上需要页面中的多个项目,但它们都在相同的标题下,我真的不想重复相同的soup_wash.find("td", headers="tf89c8e5b-5207-48e7-a536-1f50ee7f5088c{}").text.strip()
行,所以我试图将text
设置为目录以节省时间。
import requests
from bs4 import BeautifulSoup
def html(url):
return BeautifulSoup(requests.get(url).text, "lxml")
soup_wash = html("https://www.washtenaw.org/3108/Cases")
text = 'soup_wash.find("td", headers="tf89c8e5b-5207-48e7-a536-1f50ee7f5088c{}").text.strip()'
item1 = text.format("2")
item2 = text.format("6")
print(item1, item2) # Supposed to print -> 1561, 107 but it actually prints str(text) formatted.
我需要 bs4 来处理 item1 和 item2 的字符串,但我不知道该怎么做。
我个人不会使用值tf89c8e5b-5207-48e7-a536-1f50ee7f5088c{}
来获取Total Cases
数和Total Deaths
数值,因为它看起来随时会改变。
相反,获取第一个表并使用标准 python 索引来获取列。 例如:
import requests
from bs4 import BeautifulSoup
url = 'https://www.washtenaw.org/3108/Cases'
soup = BeautifulSoup(requests.get(url).content, 'html.parser')
print('{:<15}{}'.format('Total Cases', 'Total Deaths'))
for tr in soup.select('table')[0].select('tr:has(td)'):
tds = [td.get_text() for td in tr.select('td')]
print('{:<15}{}'.format(tds[1], tds[5]))
印刷:
Total Cases Total Deaths
1561 107
338 3
1899 110
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.