我需要匹配所有这些开始标记:
<p>
<a href="foo">
Run Code Online (Sandbox Code Playgroud)
但不是这些:
<br />
<hr class="foo" />
Run Code Online (Sandbox Code Playgroud)
我想出了这个,并希望确保我做对了.我只抓住了a-z.
<([a-z]+) *[^/]*?>
Run Code Online (Sandbox Code Playgroud)
我相信它说:
/,然后我有这个权利吗?更重要的是,你怎么看?
我有以下汤:
<a href="some_url">next</a>
<span class="class">...</span>
Run Code Online (Sandbox Code Playgroud)
从这里我想提取href, "some_url"
如果我只有一个标签,我可以做到,但这里有两个标签.我也可以得到文字,'next'但这不是我想要的.
此外,是否有一个很好的描述API的例子.我正在使用标准文档,但我正在寻找更有条理的东西.
我正在编写一个脚本来阅读网页,并构建一个符合特定条件的链接数据库.现在我被lxml困住,并了解如何<a href>从html中获取所有的...
result = self._openurl(self.mainurl)
content = result.read()
html = lxml.html.fromstring(content)
print lxml.html.find_rel_links(html,'href')
Run Code Online (Sandbox Code Playgroud) 我做了一些研究并看到了解决方案,但没有一个对我有用.
这不适合我.我知道0xe9是é角色.但我仍然无法弄清楚如何使这个工作,这是我的代码
output_lines = ['<menu>', '<day name="monday">', '<meal name="BREAKFAST">', '<counter name="Entreé">', '<dish>', '<name icon1="Vegan" icon2="Mindful Item">', 'Cream of Wheat (Farina)','</name>', '</dish>', '</counter >', '</meal >', '</day >', '</menu >']
output_string = '\n'.join([line.encode("utf-8") for line in output_lines])
Run Code Online (Sandbox Code Playgroud)
这给了我错误 ascii codec cant decode byte 0xe9
我试过解码,我试图取代"é"但似乎无法让它工作.
我使用以下代码(使用python和BeautifulSoup从网页检索链接获取):
import httplib2
from BeautifulSoup import BeautifulSoup, SoupStrainer
http = httplib2.Http()
status, response = http.request('http://www.nytimes.com')
for link in BeautifulSoup(response, parseOnlyThese=SoupStrainer('a')):
if link.has_attr('href'):
print link['href']
Run Code Online (Sandbox Code Playgroud)
但是,我不明白为什么我收到以下错误消息:
Traceback (most recent call last):
File "C:\Users\EANUAMA\workspace\PatternExtractor\src\SourceCodeExtractor.py", line 13, in <module>
if link.has_attr('href'):
TypeError: 'NoneType' object is not callable
Run Code Online (Sandbox Code Playgroud)
BeautifulSoup 3.2.0 Python 2.7
编辑:
我尝试了类似问题的解决方案(如果link.has_attr('href'),则输入类型错误:TypeError:'NoneType'对象不可调用),但它给出了以下错误:
Traceback (most recent call last):
File "C:\Users\EANUAMA\workspace\PatternExtractor\src\SourceCodeExtractor.py", line 12, in <module>
for link in BeautifulSoup(response).find_all('a', href=True):
TypeError: 'NoneType' object is not callable
Run Code Online (Sandbox Code Playgroud) 我正在开发一个需要从网站提取所有链接的项目,使用此代码我将从单个 URL 获取所有链接:
import requests
from bs4 import BeautifulSoup, SoupStrainer
source_code = requests.get('https://stackoverflow.com/')
soup = BeautifulSoup(source_code.content, 'lxml')
links = []
for link in soup.find_all('a'):
links.append(str(link))
Run Code Online (Sandbox Code Playgroud)
问题是,如果我想提取所有 URL,我必须编写另一个 for 循环,然后再编写一个......。我想提取该网站及其子域中存在的所有 URL。有什么办法可以做到这一点而不需要编写嵌套吗?即使使用嵌套的 for 编写,我也不知道应该使用多少个 for 来获取所有 URL。
python ×5
html ×2
decode ×1
encoding ×1
html-parsing ×1
lxml ×1
python-3.x ×1
regex ×1
tags ×1
unicode ×1
url ×1
utf-8 ×1
web-crawler ×1
web-scraping ×1
xhtml ×1