Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python获取onclick值_Python_Web Scraping_Beautifulsoup - Fatal编程技术网

Python获取onclick值

Python获取onclick值,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,我正在使用Python和BeautifulSoup为我的一个小项目创建一个网页。该网页有多个条目,每个条目由一个HTML表格行分隔。我的代码部分工作,但很多输出是空白的,它不会从网页获取所有结果,甚至不会将它们收集到同一行 <html> <head> <title>Sample Website</title> </head> <body> <table> <td class=channel>Arti

我正在使用Python和BeautifulSoup为我的一个小项目创建一个网页。该网页有多个条目,每个条目由一个HTML表格行分隔。我的代码部分工作,但很多输出是空白的,它不会从网页获取所有结果,甚至不会将它们收集到同一行

<html>
<head>
<title>Sample Website</title>
</head>
<body>

<table>
<td class=channel>Artist</td><td class=channel>Title</td><td class=channel>Date</td><td class=channel>Time</td></tr>
<tr><td>35</td><td>Lorem Ipsum</td><td><a href="#" onClick="searchDB('LoremIpsum','FooWorld')">FooWorld</a></td><td>12/10/2014</td><td>2:53:17 PM</td></tr>
</table>
</body>
</html>
我做错了什么?

试着这样做:

>>> import re
>>> for x in soup.find_all('a'):    # will give you all a tag
...     try:
...         if re.match('searchDB',x['onclick']):    # if onclick attribute exist, it will match for searchDB, if success will print
...             print x['onclick']        # here you can do your stuff instead of print
...     except:pass
... 
searchDB('LoremIpsum','FooWorld')
您可以将其保存到某个变量,如

>>> k = x['onclick']
>>> re.findall("'(\w+)'",k)
['LoremIpsum', 'FooWorld']
\w
相当于[a-zA-Z0-9]

试着这样做:

>>> import re
>>> for x in soup.find_all('a'):    # will give you all a tag
...     try:
...         if re.match('searchDB',x['onclick']):    # if onclick attribute exist, it will match for searchDB, if success will print
...             print x['onclick']        # here you can do your stuff instead of print
...     except:pass
... 
searchDB('LoremIpsum','FooWorld')
您可以将其保存到某个变量,如

>>> k = x['onclick']
>>> re.findall("'(\w+)'",k)
['LoremIpsum', 'FooWorld']
\w
相当于[a-zA-Z0-9]

试试这个

或行中的行[1:]:
cols=row.findAll('td')
link=cols[1]。查找('a')。获取('onclick')
试试这个

或行中的行[1:]:
cols=row.findAll('td')

link=cols[1]。find('a')。get('onclick')

对于您提供的输入,它会打印
['LoremIpsum','FooWorld']
,这正是您想要的,对吗?那么,问题是什么呢?请提供一个HTML输入,使您在解析时遇到困难。谢谢。对于您提供的输入,它会打印
['LoremIpsum','FooWorld']
,这正是您想要的,对吗?那么,问题是什么呢?请提供一个HTML输入,使您在解析时遇到困难。谢谢。非常感谢你的代码,这是一个了不起的改进!这正是我所需要的。。。但是,我的结果中仍然有“searchDB”。。我相信我会想出如何删除它。让所有异常保持沉默通常是不明智的做法。在这种情况下,
KeyError
就足够了。如果你知道你在做什么,那么这不是问题,是的,我们不想要没有onclick属性的“a”标签,因为它值得你投票。我仍在试图找出如何从输出中解析这个该死的“searchDB”lol@FairlightEvony我已经为您的代码提供了解决方案,这是一个了不起的改进!这正是我所需要的。。。但是,我的结果中仍然有“searchDB”。。我相信我会想出如何删除它。让所有异常保持沉默通常是不明智的做法。在这种情况下,
KeyError
就足够了。如果你知道你在做什么,那么这不是问题,是的,我们不想要没有onclick属性的“a”标签,因为它值得你投票。我仍在试图找出如何从输出中解析这个该死的“searchDB”lol@FairlightEvony我也给出了解决方案