【问题标题】:Webscraping the links from a table从表格中抓取链接
【发布时间】:2021-01-02 21:17:23
【问题描述】:

我想从表格中抓取链接及其各自的文本。我计划使用正则表达式来完成此操作。

假设在这个页面中我有多个<a href="url_i">text_i</a> 标签。我想将所有 text_i 放入一个列表中,然后将所有 href 放入一个单独的列表中。

我有:

web = requests.get(url)
web_text = web.text
texts = re.findall(r'<table .*><a .*>(.*)</a></table>, web_text)'

正则表达式在任何类的 HTML 表中查找任何类的所有锚标记并返回文本,对吗?这需要非常长的时间。这是正确的做法吗?

另外,我现在如何获取 href 网址?

【问题讨论】:

    标签: python regex web-scraping data-science


    【解决方案1】:

    我建议你使用Beautiful Soup来解析表格的HTML文本。

    改编自Beautiful Soup's documentation,例如:

    from bs4 import BeautifulSoup
    soup = BeautifulSoup(web_text, 'html.parser')
    
    for link in soup.find_all('a'):
        print(link.get('href'))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-14
      • 2021-05-03
      • 2014-05-15
      • 2018-05-09
      • 1970-01-01
      • 2017-09-07
      • 1970-01-01
      相关资源
      最近更新 更多