Python3.X BeautifulSoup([your markup], "lxml") markup_type=markup_type))的解决方案

本文介绍了一个使用Python实现的简单爬虫程序,该程序能够从维基百科的指定页面出发,随机选择链接并导航到新的文章页面。通过递归的方式,展示了如何实现一个基本的网页爬虫来遍历维基百科上的文章。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1 random.seed(datetime.datetime.now())
 2 def getLinks(articleUrl):
 3     html = urlopen("http://en.wikipedia.org"+articleUrl)
 4     bsOdj = BeautifulSoup(html)
 5     return bsOdj.find("div",{"id":"bodyContent"}).findAll("a",href=re.compile("^(/wiki/)((?!:).)*$"))
 6 links = getLinks("/wiki/Kevin_Bacon")
 7 while len(links) > 0:
 8     newArticle = links[random.randint(0,len(links)-1)].attrs["href"]
 9     print(newArticle)
10     links = getLinks(newArticle)

这是我的源代码,然后报了警告

D:\Anaconda3\lib\site-packages\bs4\__init__.py:181: UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("lxml"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.

The code that caused this warning is on line 16 of the file D:/ThronePython/Python3 网络数据爬取/BeautifulSoup 爬虫_开始爬取/BeautifulSoup 维基百科六度分割_构建从一个页面到另一个页面的爬虫.py. To get rid of this warning, change code that looks like this:

 BeautifulSoup([your markup])

to this:

 BeautifulSoup([your markup], "lxml")

  markup_type=markup_type))

百度后发现,其实这是没有设置默认的解析器造成的,

根据提示设置解析器即可,否则则采取默认的解析器,将第四行改为:

    bsOdj = BeautifulSoup(html,"lxml")

即可.

我的csdn博客地址:http://blog.youkuaiyun.com/fontthrone/article/details/70429776

 

转载于:https://www.cnblogs.com/fonttian/p/6596795.html

在天津,上述代码块后打印到第六页的时候出现异常,ypeError Traceback (most recent call last) Cell In[51], line 35 33 # 传递正确URL到爬取函数 34 html = getHtmlDoc(current_url) ---> 35 page_comment = getComment(html) 36 all_comment.extend(page_comment) 37 time.sleep(3) # 延时3秒,防止被禁 Cell In[51], line 18, in getComment(html) 16 def getComment(html): 17 comment=[] ---> 18 soup = BeautifulSoup(html, &#39;html.parser&#39;)#使用 BeautifulSoup 库将传入的 HTML 文档解析成一个 soup 对象。 19 #这里指定使用 html.parser 作为解析器。 20 #soup = BeautifulSoup(html, &#39;lxml&#39;)#使用 lxml 库解析 HTML 文档,速度更快。 21 comment = soup.find_all("p", class_="comment-content") File ~\anaconda3\Lib\site-packages\bs4\__init__.py:315, in BeautifulSoup.__init__(self, markup, features, builder, parse_only, from_encoding, exclude_encodings, element_classes, **kwargs) 313 if hasattr(markup, &#39;read&#39;): # It&#39;s a file-type object. 314 markup = markup.read() --> 315 elif len(markup) <= 256 and ( 316 (isinstance(markup, bytes) and not b&#39;<&#39; in markup) 317 or (isinstance(markup, str) and not &#39;<&#39; in markup) 318 ): 319 # Issue warnings for a couple beginner problems 320 # involving passing non-markup to Beautiful Soup. 321 # Beautiful Soup will still parse the input as markup, 322 # since that is sometimes the intended behavior. 323 if not self._markup_is_url(markup): 324 self._markup_resembles_filename(markup) TypeError: object of type &#39;NoneType&#39; has no len()。
最新发布
03-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值