Python 使用Beauty soup仅打印链接的特定部分
我正在使用BeautifulSoup使用以下代码解析一个网站。我能够解析网站和打印数据,但是,我只想打印链接中的一部分数据。有人能提供如何做到这一点的意见吗Python 使用Beauty soup仅打印链接的特定部分,python,beautifulsoup,Python,Beautifulsoup,我正在使用BeautifulSoup使用以下代码解析一个网站。我能够解析网站和打印数据,但是,我只想打印链接中的一部分数据。有人能提供如何做到这一点的意见吗 from bs4 import BeautifulSoup as bs import argparse import urllib import urllib2 import getpass import re import requests def update (url): print url req = urllib
from bs4 import BeautifulSoup as bs
import argparse
import urllib
import urllib2
import getpass
import re
import requests
def update (url):
print url
req = urllib2.Request(url=url)
try:
f = urllib2.urlopen(req)
txt = f.read()
soup = bs(txt)
print soup
f.close()
def main ():
#For logging
print "test"
parser = argparse.ArgumentParser(description='This is the update.py script created by test')
parser.add_argument('-u','--url',action='store',dest='url',default=None,help='<Required> url link',required=True)
results = parser.parse_args()# collect cmd line args
url = results.url
#print url
update(url)
if __name__ == '__main__':
main()
从bs4导入美化组作为bs
导入argparse
导入URL库
导入urllib2
导入getpass
进口稀土
导入请求
def更新(url):
打印url
请求(url=url)
尝试:
f=urllib2.urlopen(请求)
txt=f.read()
汤=bs(txt)
印花汤
f、 关闭()
defmain():
#用于记录
打印“测试”
parser=argparse.ArgumentParser(description='这是由test'创建的update.py脚本)
parser.add_参数('-u','-url',action='store',dest='url',default=None,help='url link',required=True)
结果=parser.parse_args()#collect cmd line args
url=results.url
#打印url
更新(url)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()
以下是电流输出。预期结果如下所示
current output :-
==== Test results ====
results are in \\data\loc
==== <font color="#008000">Build Combo</font> ====
{| border="1" cellspacing="1" cellpadding="1"
|-
! bgcolor="#67B0F9" scope="col" | test1
! bgcolor="#67B0F9" scope="col" | test2
! bgcolor="#67B0F9" scope="col" | test3
! bgcolor="#67B0F9" scope="col" | test4
|-
| [http:link.com]
|}
==== <font color="#008000">COde:</font> ====
Expected output:-
==== <font color="#008000">Build Combo</font> ====
{| border="1" cellspacing="1" cellpadding="1"
|-
! bgcolor="#67B0F9" scope="col" | test1
! bgcolor="#67B0F9" scope="col" | test2
! bgcolor="#67B0F9" scope="col" | test3
! bgcolor="#67B0F9" scope="col" | test4
|-
| [http:link.com]
|}
电流输出:-
==测试结果====
结果显示在\\data\loc中
==构建组合====
{| border=“1”cellspacing=“1”cellpadding=“1”
|-
!bgcolor=“#67B0F9”scope=“col”| test1
!bgcolor=“#67B0F9”scope=“col”| test2
!bgcolor=“#67B0F9”scope=“col”| test3
!bgcolor=“#67B0F9”scope=“col”| test4
|-
|[http:link.com]
|}
==代码:====
预期产出:-
==构建组合====
{| border=“1”cellspacing=“1”cellpadding=“1”
|-
!bgcolor=“#67B0F9”scope=“col”| test1
!bgcolor=“#67B0F9”scope=“col”| test2
!bgcolor=“#67B0F9”scope=“col”| test3
!bgcolor=“#67B0F9”scope=“col”| test4
|-
|[http:link.com]
|}
我会第一个承认我不太确定你在问什么,但我认为如果我没弄错的话,你只想打印一个元素,而不是整个汤。如果是这种情况,您需要找到要打印的元素,然后打印它。假设您正在查找元素,那么更新方法可能更像这样
def update (url):
print url
req = urllib2.Request(url=url)
try:
f = urllib2.urlopen(req)
txt = f.read()
soup = bs(txt)
a_element = soup.find("a")
print a_element
f.close()
你最终解决了这个问题吗?