bs处理后的源代码用正则表达式需要转化格式

#抓取主页面热点新闻,写入tengxun_news.txt
def get_main_page():
    resp_main_page = get_html(main_page_utl)
    soup_1 = BeautifulSoup(resp_main_page.text,"lxml")
    item_1s = soup_1.select(".linkto")
    print(item_1s)
    print("-------------------------------------------------------------------")
    html = item_1s
    pattern = re.compile('<a class="linkto" href=(.*?) target="_blank">(.*?)</a>,', re.S)
    items = re.findall(pattern, html)

    print(items)

用beautifulsoup抓取下来的内容自动保存为list格式,想要继续用正则表达式提取,需要转化成字符串的形式。 


#抓取主页面热点新闻,写入tengxun_news.txt
def get_main_page():
resp_main_page = get_html(main_page_utl)
soup_1 = BeautifulSoup(resp_main_page.text,"lxml")
item_1s = soup_1.select(".linkto")
print(item_1s)
print("-------------------------------------------------------------------")
html = str(item_1s)
pattern = re.compile('<a class="linkto" href=(.*?) target="_blank">(.*?)</a>,', re.S)
items = re.findall(pattern, html)

print(items)

参考:https://blog.csdn.net/weixin_42105977/article/details/80390957

猜你喜欢

转载自blog.csdn.net/junekakui/article/details/81041120