【发布时间】:2020-06-17 06:36:22
【问题描述】:
我目前正在尝试从网页上抓取一些图表,但我是新手,不知道最佳解决方案。
<font color="DarkBLUE">
g:=Graph<5|{ {2, 3}, {4, 5}, {1, 3}, {1, 2}, {1, 5}, {1, 4}, {2, 4}, {3, 5}, {2,
5}, {3, 4} }>;</font>
我需要的是g:=Graph<..> 部分。
这是我到目前为止尝试过的(基于其他一些类似的问题):
tree = lh.fromstring(data)
rate = tree.xpath("//font[@color='DarkBLUE']")
graphurls.append(rate[0].text_content())
但问题是它会刮掉很多其他的东西。我认为这是可以做到的,因为它有一个独特的模式g:=Graph<...>,所以没有其他东西会被刮掉。
你能帮帮我吗?
【问题讨论】:
-
if text.strip().startswith("g:=Graph"): print(text.strip()[8:-2])? -
但问题是它会刮掉很多其他东西。 还有什么其他东西?请显示输出,尤其是因为您还没有分享minimal reproducible example。
标签: python regex python-3.x web-scraping beautifulsoup