[英]How can I print a formatted array in python 3.x?
I trying to write a web-scraper for wordreference.com, the code is this:我试图为 wordreference.com 编写一个网络爬虫,代码是这样的:
import bs4,sys, requests
from urllib import request
def scraper():
url=f"https://wordreference.com/iten/{sys.argv[1]}"
r=requests.get(url)
html=r.text
soup=bs4.BeautifulSoup(html, "html.parser")
x=soup.find_all("td","ToWrd")
print(x[1].contents)
if __name__ == "__main__":
scraper()
The code is right but the output is this: input:代码是正确的,但 output 是这样的:输入:
python3 scraper.py cane
Output: Output:
['dog ', <em class="tooltip POS2">n<span><i>noun</i>: Refers to person, place, thing, quality, etc.</span></em>]]
I want print only the first part of array, when is contained 'dog'.我只想打印数组的第一部分,什么时候包含“狗”。 How can I resolve this problem?
我该如何解决这个问题?
well here is the code:那么这里是代码:
print(x[1].contents[0])
x[1].contents returns a list, so to access the string 'dog ', you need to specify index 0 x[1].contents 返回一个列表,因此要访问字符串 'dog',您需要指定索引 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.