import requests from bs4 import BeautifulSoup url = 'https://b.guidaye.com/changxiao/9948/' def getCpontent(url): response = requests.get(url).content.decode('gbk',errors='ignore') soup = BeautifulSoup(response,'html.parser') content = soup.find('div',class_='mb2').text return content def getTitleLink(url): html = requests.get(url).content.decode('utf-8') soup = BeautifulSoup(html,'html.parser') chapters = soup.find('div',class_='book-list mb clearfix') chapters = chapters.find_all('a') titleLink = {} for each in chapters: title = each.text link = ('https://b.guidaye.com/'+each.get('href')) titleLink[title] = link return (titleLink) def main(): titleLink = getTitleLink(url) for title,link in titleLink.items(): f = open('天才在左疯子在右.txt','a') print(title) f.write(title) f.write(getCpontent(link)) f.close() main() 哪里出问题了
时间: 2024-04-02 15:34:46 浏览: 164
在代码的最后一行,应该是调用 `main()` 函数,而不是在 `main()` 函数内部调用自身。此外,建议在写代码时注意缩进,使代码更易读易懂。修改后的代码如下:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://b.guidaye.com/changxiao/9948/'
def getContent(url):
response = requests.get(url).content.decode('gbk',errors='ignore')
soup = BeautifulSoup(response,'html.parser')
content = soup.find('div',class_='mb2').text
return content
def getTitleLink(url):
html = requests.get(url).content.decode('utf-8')
soup = BeautifulSoup(html,'html.parser')
chapters = soup.find('div',class_='book-list mb clearfix')
chapters = chapters.find_all('a')
titleLink = {}
for each in chapters:
title = each.text
link = ('https://b.guidaye.com/'+each.get('href'))
titleLink[title] = link
return titleLink
def main():
titleLink = getTitleLink(url)
for title,link in titleLink.items():
f = open('天才在左疯子在右.txt','a')
print(title)
f.write(title)
f.write(getContent(link))
f.close()
main()
```
阅读全文