需要一些帮助在使用beautifulsouplibrary网站垃圾。你知道吗
我需要从网页http://thehill.com/…/365407-sean-diddy-combs-wants-to-buy-c提取文本
我的目标是提取的文本完全一样,在网页上,我提取所有的“p”标签和它的文本,但在“p”标签里面有“a”标签,其中也有一些文本。你知道吗
所以我的问题是: 1如何将unicoded(“”)转换成普通字符串作为网页中的文本?因为当我只提取“p”标记时,beautifulsoup库会将文本转换为unicoded,甚至特殊字符也会被unicoded,所以我想将提取的unicoded文本转换为普通文本。我该怎么做?你知道吗
我尝试了以下代码:
html = requests.get("http://thehill.com/…/365407-sean-diddy-combs-wants-to-buy-c…").content
news_soup = BeautifulSoup(html, "html.parser")
a_text = news_soup.find_all('p')
y = a_text[1].find_all('a').string
您可以使用嵌套列表理解来查找带有段落标记的所有链接,并使用
encode("ascii", 'ignore')
对unicode进行解码:输出:
相关问题 更多 >
编程相关推荐