Answer a question

I tried to use BeautifulSoup to check if website updated. So, I want to know what will the program get from my code. But after I ran my code:

import requests

html = requests.get("https://www.mangabz.com/60bz/")
soup = BeautifulSoup(html.text, "html.parser")
code = soup.prettify()
result = code.find('<div class="container">')
print(result)

It responded a number: 1612

But I thought it should be like:

<a href="/m168608/" title="211試看版" style="color:#ff3f60;" target="_blank">211試看版 </a></span></span>]

Why?

Answers

You need to pass tag name as first argument. Other attributes of tag are passed as a dictionary. Don't directly pass markup as argument

import requests
from bs4 import  BeautifulSoup

html = requests.get("https://www.mangabz.com/60bz/")
soup = BeautifulSoup(html.text, "html.parser")
result = soup.find('div',{'class':'container'})

print(result)

It responded a number: 1612

The mistake is on these lines :

code = soup.prettify()
result = code.find('<div class="container">')

prettify method returns a string not a BeautifulSoup object as you might have anticipated. string also has a find method which returns index of first occurrence of the substring (if found). In your case it found the match at index 1612.

Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐