帮我把上面提取的chaind_data加到下面代码中def Gethtml(self,i): self.url = f'https://www.qidian.com/rank/hotsales/chn&{data-chanid}/page{page}/' response = self.Uresponse().content.decode() tree = etree.HTML(response) li_list = tree.xpath('//*[@id="book-img-text"]/ul/li') for li in li_list: url_list = 'https:' + str(li.xpath('./div[3]/p/a[1]/@href')[0]) self.Gettypehtml(url_list)
时间: 2024-04-17 08:23:53 浏览: 190
当你想将提取的`data_chanid`值加入到`Gethtml`方法中的URL中时,你可以将提取到的`data_chanid`值保存到一个实例变量中,然后将其添加到URL中。以下是修改后的代码示例:
```python
import requests
from lxml import etree
class YourClassName:
def __init__(self):
self.url = None
self.data_chanid = "-1"
self.page = 1
def Gethtml(self, i):
self.url = f'https://www.qidian.com/rank/hotsales/chn{self.data_chanid}/page{self.page}/'
response = self.Uresponse().content.decode()
tree = etree.HTML(response)
li_list = tree.xpath('//*[@id="book-img-text"]/ul/li')
for li in li_list:
url_list = 'https:' + str(li.xpath('./div[3]/p/a[1]/@href')[0])
self.Gettypehtml(url_list)
def Uresponse(self):
response = requests.get(self.url)
return response
```
在这个示例代码中,我们在类的初始化方法`__init__`中添加了一个实例变量`data_chanid`,并将其初始值设置为`"-1"`。然后,在`Gethtml`方法中,我们将提取到的`data_chanid`值添加到URL中的`{chanid}`占位符位置。最后,我们使用修改后的URL发送请求并获取网页内容。
请注意,以上代码仅提供了一个示例,你可能需要根据实际情况进行适当的调整和扩展。
阅读全文